package api.homework.two.two;

import java.util.Random;

/**
 * @author lwj
 * @date 2025-03-11
 * @vesion 1.0.0
 */
public class SellingTicketsRunnable implements Runnable{
    /**
     * 总票数
     */
    private int ticket = 50;

    // 构造方法
    public SellingTicketsRunnable() {}

    @Override
    public synchronized void run() {
        while (true) {
            try {
                Thread.sleep(200);
                // 每个人中间买票间隔2秒
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖了1张票，还剩" + --ticket + "张票");
            } else {
                break;
            }
        }
        System.out.println("票卖完了！");
    }
}
